<?php
/*
//  Copyright (C) 2007 Max Plischke
//  Original Author: Max Plischke <plischke@gmail.com>
//  Link: http://code.google.com/p/open-club-directory/
// 
//  This file is part of the Open-Club-Directory Project.
//
//  Open-Club-Directory is free software: you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Open-Club-Directory is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Open-Club-Directory.
//  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'appinclude.php';
require_once 'appinclude_require_add.php';
require_once 'funcinclude.php';

$form_country = $_REQUEST['country'];
$form_club = $_REQUEST['club'];

settype($form_country, "float");
settype($form_club, "float");

// -----

$link = mysql_connect($_mysql_server, $_mysql_user, $_mysql_password) or die('Could not connect: ' . mysql_error());
mysql_select_db($_mysql_db) or die('Could not select database');

if ($is_app_added==true) { 
	$member = member($user);	
} else { 
	$member=false; 
}

if ($member==false) {

	if (empty($form_country) && empty($form_club)) {
		$html .= ' <input type="submit" value="proceed" /> or <a href="'.$appcallbackurl.'">Cancel</a><br>';
	} elseif (!empty($form_country) && empty($form_club)) {
		$html .= ' <input type="submit" value="Save Membership" /> or <a href="'.$appcallbackurl.'">Cancel</a><br>';
	} else {
		// process form
		$query = sprintf("INSERT INTO users (fb_id, club_id, verified, ts) VALUES (%d, %d, 1, NOW())",
			$user,
			$form_club);
		$result = mysql_query($query) or die();
		
		$member = member($user);
		
		// update profile 
		$facebook->api_client->profile_setFBML('', $user, profile($member), '', '');

		// publish feed
		$feed = feed('club', $member);
		$facebook->api_client->feed_publishTemplatizedAction($feed[title_template], $feed[title_data], $feed[body_template], $feed[body_data], '');
    	
    	mysql_close($link);
		
		echo '<fb:redirect url="'.$appcallbackurl.'?selected_club=1">';
		exit;
	}
	
	echo '<fb:header decoration="add_border">'.$_ClubName.' Application Setup</fb:header><fb:error message="Select your '.$_ClubName.' Club Membership"><b><font color="#CC0000">Caution</font></b>: Only proceed if you really are a '.$_ClubName.' Member!</fb:error>';
	echo '<form action="add.php?do-it" method="get">';
	echo '<div style="padding: 5px 10px 5px 20px">';
	echo '<select name="country">';
	echo '<option value="0">select your clubs country ...</option>';
    echo dropdown(countries('inDatabase'), $form_country);
    echo '</select>';
    
    if (!empty($form_country)) {
		echo '<br><br><select name="club">';
		echo '<option value="0">select your club ...</option>';
		echo dropdown(clubs('country', $form_country), $form_club);
   		echo '</select>';
   		echo ' <small>or <a href="'.$appcallbackurl.'addclub.php">Add a New Club</a>!</small>';
	} else {
		echo ' <small>or <a href="'.$appcallbackurl.'addclub.php">Add a Club</a> in a New Country!</small>';
	}
	
	echo '<br><br>';
	echo $html;
	
	echo '</div>';
	echo '</form><br><br>';
} else {
	echo '<fb:redirect url="'.$appcallbackurl.'edit.php">';
}
mysql_close($link);

?>
<fb:google-analytics uacct="<?php echo $_google_analytics_account; ?>" />